<template>
  <div class="mod-user">
    <div style="margin-bottom: 12px;padding-left: 6px">
      <el-button icon="el-icon-plus" type="primary" size="small" @click.stop="add">新增管理员</el-button>
      <el-button size="small" @click.stop="back">返回</el-button>
    </div>
    <avue-crud ref="crud"
               :page="page"
               :data="dataList"
               :option="tableOption"
               @current-change="currentChange"
               @size-change="sizeChange">

      <template slot-scope="scope"
                slot="menu">
        <el-button type="primary"
                   icon="el-icon-edit"
                   size="small"
                   @click.stop="addOrUpdateHandle(scope.row)">编辑</el-button>
        <el-button type="danger" icon="el-icon-delete" size="small" @click.stop="deleteHandle(scope.row.id)">删除</el-button>
      </template>
    </avue-crud>
    <userAddOrUpdate ref="userAddOrUpdate" @refreshDataList="getDataList"></userAddOrUpdate>
  </div>
</template>

<script>
import { tableOption } from '@/crud/factory/factoryUser'
import userAddOrUpdate from './user-add-or-update'
export default {
  components: {
    userAddOrUpdate
  },
  data () {
    return {
      factoryId: '',
      dataList: [],
      dataListLoading: false,
      tableOption: tableOption,
      page: {
        total: 0, // 总页数
        currentPage: 1, // 当前页数
        pageSize: 10 // 每页显示多少条
      },
    }
  },

  mounted () {
    this.factoryId = this.$route.query.id
    console.log(this.factoryId)
    this.getDataList()
  },

  methods: {
    // 获取数据列表
    getDataList (page, params) {
      this.dataListLoading = true
      this.$http({
        url: this.$http.adornUrl('/v1/factory/user/list'),
        method: 'get',
        params: this.$http.adornParams(
          Object.assign(
            {
              current: page == null ? this.page.currentPage : page.currentPage,
              size: page == null ? this.page.pageSize : page.pageSize,
              factoryId: this.factoryId
            },
            params
          )
        )
      }).then(({ data }) => {
        console.log(data)
        this.dataList = data.data.records
        this.page.total = data.data.total
        this.dataListLoading = false
      })
    },
    // 删除
    deleteHandle (id) {
      this.$confirm(`确定进行删除操作?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        let param = {
          id: id
        }
        this.$http({
          url: this.$http.adornUrl('/v1/factory/user/delete'),
          method: 'post',
          data: this.$http.adornData(param)
        }).then(({ data }) => {
          this.$message({
            message: '操作成功',
            type: 'success',
            duration: 1500,
            onClose: () => {
              this.getDataList(this.page)
            }
          })
        })
      }).catch(() => { })
    },
    // 新增
    add () {
      this.$refs.userAddOrUpdate.init(this.factoryId)
    },

    back(){
      this.$router.back()
    },

    addOrUpdateHandle (row) {
      let r = JSON.parse(JSON.stringify(row))
      console.log(r)
      this.$refs.userAddOrUpdate.init(this.factoryId, r)
    },
    currentChange(val){
      this.page.currentPage=val
      console.log(val)
      this.getDataList(this.page,this.formData)
    },
    sizeChange(val){
      this.page.pageSize=val
      this.getDataList(this.page,this.formData)
    }
  }
}
</script>

<style lang="scss">
.remain{
  display: flex;
  justify-content: flex-start;
  align-items: center;
  input{
    width: 100px;
  }
}
.sumary-list{
  display: flex;
  justify-content: flex-start;
  margin-bottom: 15px;
  div{
    height: 30px;
    padding: 0px 12px;
    line-height: 30px;
    border:solid 1px #009FF1;
    border-radius: 15px;
    margin-right: 10px;
  }
  div:hover{
    color: white;
    background-color: #009FF1;
  }
}
</style>
